// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

Rectangle {
    id: mainRoot
    width: 800
    height: 600
    color: "lightgrey"

    property string curState: "Waiting"
    property string backupImageName: ""

    function showProc(val) {
        proc.val = val;
    }

    function showSpeed(speed) {
        optSpeed.text = speed;
    }

    function showSpendTime(spendTime) {
        optSpendTime.text = spendTime;
    }
    function showRemainingTime(remainTime) {
        optRemainTime.text = remainTime;
    }

    function showOptDev(devName) {
        optDevName.text = devName;
    }

    function getBackupImageName()
    {
        var d = new Date();
        var years = d.getFullYear();
        var month = add_zero(d.getMonth()+1);
        var days = add_zero(d.getDate());
        var hours = add_zero(d.getHours());
        var minutes = add_zero(d.getMinutes());
        var seconds=add_zero(d.getSeconds());
        var ndate = years+"-"+month+"-"+days+"-"+hours+"-"+minutes+"-"+seconds;
        return "Intel-"+ndate+"-"+workTool.getDiskSize()+"-image";
    }

    function add_zero(temp)
    {
        if(temp<10) return "0"+temp;
        else return temp;
    }

    Rectangle {
        id: bkgTop
        x: parent.x
        y: parent.y
        width: parent.width
        height: 85
        color: "#226ab3"

        Text {
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            text: "Image Recovery Utility"
            font {pixelSize: 30 }
            color: "white"
        }
    }

    Rectangle {
        id: bkgBottom
        anchors.top: bkgTop.bottom
        anchors.left: bkgTop.left
        anchors.bottom: parent.bottom
        anchors.right: bkgTop.right
        color: "#cde1ed"
    }

    Image {
        id: shutDownBtn
        y: (bkgTop.height - bkgTop.y - height) / 2; x: mainRoot.width - 40 - width
        source: "images/shutdown-normal.png"

        MouseArea {
            anchors.fill: parent
            onPressed:  {
                parent.source = "images/shutdown-press.png";
            }
            onReleased: {
                parent.source = "images/shutdown-normal.png";
            }
            onClicked: {
                workTool.shutDownDevice();
            }
        }
    }

    Item {
        id: page1
        visible: true

        Rectangle {
            id: btnBkp
            x: bkgBottom.x
            y: bkgBottom.y
            height: bkgBottom.height
            width: bkgBottom.width / 2
            color: "transparent"

            Image {
                id: bkgImage
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                source: "images/backup-normal.png"
            }
            Text {
                anchors.horizontalCenter: parent.horizontalCenter
                y: bkgImage.y + bkgImage.height + 25
                text: "Backup System"
                font { pixelSize: 20;}
            }
            MouseArea {
                anchors.fill: parent
                onPressed: {
                    bkgImage.source = "images/backup-press.png"
                }
                onReleased: {
                    bkgImage.source = "images/backup-normal.png"
                }
                onClicked: {
                    curState = " Backing Up ..."
                    page1.visible = false;
                    page2.visible = true;
                    workTool.setStatus(1);
                    backupImageName = getBackupImageName();
                    workTool.execBashFile("bash /Backup.sh " + backupImageName);
                    console.log("Clicked to backup:"+backupImageName);
                    timer1.start();
                    proc.val = 1;
                }
            }
        }
        Image {
            x: btnBkp.width + btnBkp.x - width / 2
            y: bkgBottom.height / 2 + bkgBottom.y - height / 2
            width: 1
            height: 234
            source: "images/split-line.png"

        }

        Rectangle {
            id: btnRst
            anchors.left: btnBkp.right
            anchors.top: btnBkp.top
            anchors.bottom: btnBkp.bottom
            width: btnBkp.width
            color: "transparent"
            Image {
                id: rstImage
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                source: "images/restore-normal.png"
            }
            Text {
                anchors.horizontalCenter: parent.horizontalCenter
                y: rstImage.y + rstImage.height + 25
                text: "Restore System"
                font { pixelSize: 20}
            }
            MouseArea {
                anchors.fill: parent
                onPressed: {
                    rstImage.source = "images/restore-press.png"
                }
                onReleased: {
                    rstImage.source = "images/restore-normal.png"
                }
                onClicked: {
                    var imageDir = workTool.getImageDir();
                    if (0 === imageDir.length) {
                        return;
                    }
                    curState = " Restoring ..."
                    page1.visible = false;
                    page2.visible = true;
                    workTool.setStatus(2);
                    workTool.execBashFile("bash /Restore.sh " + imageDir);
                    console.log("Clicked to restore");
                    timer1.start();
                    proc.val = 1;

                }
            }
        }
    }
    Rectangle {
        id: page2
        visible: false
        anchors.fill: bkgBottom
        color: "transparent"

        Rectangle {
            id: proc
            anchors.horizontalCenter: parent.horizontalCenter
            y: 50
            color: "#cccccc"
            border.color: "grey"
            border.width: 1
            radius: 10
            smooth: true
            width: 600
            height: 6
            property int val: 0

            Rectangle {
                color: "#0099cc"
                radius: parent.radius
                smooth: parent.smooth
                height: parent.height
                width: parent.width * parent.val / 100
            }
        }

        Rectangle {
            id: btnCancel
            x: mainRoot.width - 40 - width
            y: parent.height - 20 - height
            width: 99
            height: 39
            color: "transparent"
            Image {
                source: "images/btn-bkg.png"
            }
            Text {
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                text: "Cancel"
                font.pixelSize: 20
            }
            MouseArea {
                anchors.fill: parent
                onPressed: {
                    btnCancel.color = "yellow"
                }
                onReleased: {
                    btnCancel.color = "white"
                }
                onClicked: {
                    var rst = workTool.confirmCancel();
                    console.log("rst: " + rst)
                    if (false === rst)
                    {
                        return;
                    }
                    workTool.execBashFile("rm -rf /home/partimag/"+backupImageName)
                    console.log("rm -rf /home/partimag/"+backupImageName);
                    workTool.setStatus(0);
                    workTool.stopCurrentTask();
                    console.log("Clicked to cancel");
                    page1.visible = true;
                    page2.visible = false;
                    timer1.stop();
                    curState = " Stoping By User"
                    proc.val = 0;
                    logsModel.clear();
                    optDevName.text = "";
                    optSpeed.text = "";
                    optSpendTime = "";
                    optRemainTime = "";
                }
            }
        }

        Rectangle {
            id: logSwitch
            anchors.right: proc.left
            anchors.top: proc.bottom
            color: "black"
            width:10;
            height: 10

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    if (0 === logs.height) {
                        logs.height = btnCancel.y - logs.y - 20;
                    }
                    else {
                        logs.height = 0;
                    }
                }
            }
        }

        Text {
            id: showCurStatus
            anchors.left: proc.left
            anchors.bottom: proc.top
            text: curState
            font.pixelSize: 20
        }
        Row {
            spacing: 10
            layoutDirection: Qt.RightToLeft
            anchors.left: showCurStatus.right
            anchors.right: proc.right
            anchors.bottom: proc.top
            Text {
                id: optDevName
                text: ""

            }
            Text {
                id: optSpendTime
                text:""
            }
            Text {
                id: optRemainTime
                text:""
            }

            Text {
                id: optSpeed
                text: "0M/min"
            }
        }

        ListModel {
            id: logsModel
        }

        Component {
            id: myDelegate
            Text { text: log }
        }

        ListView {
            id: logs
            clip: true

            anchors.horizontalCenter: parent.horizontalCenter
            y: proc.y + proc.height + 20
            width: proc.width
            height: btnCancel.y - y - 20
            model: logsModel
            delegate: myDelegate

            Behavior on height {
                PropertyAnimation {duration: 1000}
            }
        }


        Timer {
            id: timer1
            interval: 2000
            repeat: true
            onTriggered: {
                var output = workTool.readBashOutput1();
                if (0 === output.length) {
                    return;
                }
                if (output.indexOf("WWW") >= 0)
                {
                    return;
                }
                if (logsModel.count < 50) {
                    logsModel.append({"log": output});
                    logs.currentIndex = logsModel.count -1;
                    logs.positionViewAtIndex(logs.currentIndex, ListView.End);
                }
                else {
                    logsModel.clear();
                    logsModel.append({"log": output});
                    logs.currentIndex = logsModel.count -1;
                    logs.positionViewAtIndex(logs.currentIndex, ListView.End);
                }
            }
        }
    }
}
